<?php
/**
 * Created by PhpStorm.
 * User: Dream
 * Date: 2017/12/2
 * Time: 16:06
 */

namespace App\Http\Controllers\Index;


use App\Base\BaseFunc;
use Illuminate\Routing\Controller;
use App\Base\OrderStatus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class OrderController extends Controller
{
    public function order_goods(OrderStatus $orderStatus,BaseFunc $baseFunc)
    {
        $value = Input::only("goods_id");
        $input['order_goods'] = $value['goods_id'];
        $input['order_status'] = $orderStatus->unOrder()->status_id;
        $input['order_time'] = date('Y-m-d H:i:s');
        $input['order_user'] = session('user_id');
        DB::table('order')->insert($input);
        $buy['buy_goods'] = $value['goods_id'];
        $buy['buy_user'] = session('user_id');
        $buy['buy_time'] = date('Y-m-d H:i:s');
        DB::table('buy_num')->insert($buy);
        $num['goods_buy_num'] = DB::table('buy_num')
            ->where('buy_goods',$value['goods_id'])
            ->count();
        DB::table('goods')
            ->where('goods_id',$value['goods_id'])
            ->update($num);
        $baseFunc->setRedirectMessage(true,'支付成功,可到我的订单查看！',NULL,NULL);
        return redirect()->back();
    }

    public function goods_order()
    {
        $data['orderData'] = DB::table('order')
            ->leftJoin('goods','goods_id','=','order_goods')
            ->leftJoin('order_status','status_id','=','order_status')
            ->where('order_user','=',session('user_id'))
            ->get();
        return view("/index.goodsOrder",$data);
    }

    public function toOrder(OrderStatus $orderStatus,BaseFunc $baseFunc)
    {
        $value = Input::only("order_id");
        $eva['evaluate_id'] = $value['order_id'];
        $input['order_status'] = $orderStatus->Ordered()->status_id;
        $check = DB::table('order')
            ->where('order_id','=',$value['order_id'])
            ->update($input);
        if($check)
        {
            DB::table('evaluate')->insert($eva);
            $baseFunc->setRedirectMessage(true,'确认收货成功,宝贝可还满意？去评价一番吧！',NULL,"/goods_order");
            //return redirect()->back();
        }

    }

    public function addEva(OrderStatus $orderStatus,BaseFunc $baseFunc)
    {
        $value = Input::only('order_id','evaluate_info','goods_id');
        $input['order_status'] = $orderStatus->end()->status_id;
        $eva['evaluate_info'] = $value['evaluate_info'];
        $eva['evaluate_user'] = session('user_id');
        $eva['evaluate_time'] = date("Y-m-d H:i:s");
        $eva['evaluate_goods'] = $value['goods_id'];
        $check = DB::table('order')
            ->where('order_id','=',$value['order_id'])
            ->update($input);
        if($check)
        {
            DB::table('evaluate')
                ->where('evaluate_id','=',$value['order_id'])
                ->update($eva);
            $baseFunc->setRedirectMessage(true,'评价完成！',NULL,"/goods_order");
            //return redirect()->back();
        }
    }
}